package com.qing.emailservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qing.emailservice.common.utils.RedisUtil;
import com.qing.emailservice.entity.pojo.UcenterMember;
import com.qing.emailservice.entity.vo.LoginVo;
import com.qing.emailservice.entity.vo.RegisterVo;
import com.qing.emailservice.mapper.UcenterMemberMapper;
import com.qing.emailservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.servicebase.exception.JsonException;
import com.qing.servicebase.utils.CheckTool;
import com.qing.servicebase.utils.JwtUtils;
import com.qing.servicebase.utils.ResponseResult;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Yjx
 * @since 2022-09-14
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    private final UcenterMemberMapper memberMapper;

    private final RedisUtil redisUtil;

    private final BCryptPasswordEncoder bCryptPasswordEncoder;

    public UcenterMemberServiceImpl(UcenterMemberMapper memberMapper, RedisUtil redisUtil, BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.memberMapper = memberMapper;
        this.redisUtil = redisUtil;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }

    @Override
    public ResponseResult login(LoginVo loginVo) {
        String email = loginVo.getEmail();
        String password = loginVo.getPassword();
        UcenterMember menberExists = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("email", email));
        if (menberExists == null) {
            return ResponseResult.ofException(new JsonException(201, "没有该用户"));
        }
        boolean matches = bCryptPasswordEncoder.matches(password, menberExists.getPassword());
        if(!matches){
            return ResponseResult.ofException(new JsonException(201, "账户密码错误"));
        }
        String jwtToken = JwtUtils.getJwtToken(menberExists.getId(), menberExists.getNickname());
        return ResponseResult.ofSuccess(jwtToken);
    }

    @Override
    public ResponseResult register(RegisterVo registerVo) {
        String email = registerVo.getEmail();
        String code = registerVo.getCode();
        if (!CheckTool.isEmail(email)) {
            return ResponseResult.ofException(new JsonException(201, "邮箱格式错误"));
        }
        /**
         * TODO
         * 检验邮箱是否已经注册存在
         */
        Integer count = baseMapper.selectCount(
                new QueryWrapper<UcenterMember>().eq("email", email));
        if (count.intValue() > 0) {
            return ResponseResult.ofException(new JsonException(201, "改邮箱注册过"));
        }
        String redisCode = (String) redisUtil.get("register" + email);
        if (redisCode == null) {
            return ResponseResult.ofException(new JsonException(201, "验证码过期"));
        }
        if (!redisCode.equals(code)) {
            return ResponseResult.ofException(new JsonException(201, "验证码错误"));
        }
        redisUtil.del("register" + email);
        //创建插入数据库用户对象
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setNickname(registerVo.getNickname());
        ucenterMember.setEmail(registerVo.getEmail());
        ucenterMember.setPassword(bCryptPasswordEncoder.encode(registerVo.getPassword()));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("https://edu-serviceq.oss-cn-hangzhou.aliyuncs.com/avatar/file.jpg");
        ucenterMember.setGmtCreate(LocalDateTime.now());
        ucenterMember.setGmtModified(LocalDateTime.now());
        int insert = memberMapper.insert(ucenterMember);
        return insert > 0 ? ResponseResult.ofSuccess("注册成功") :
                ResponseResult.ofException(new JsonException(201, "注册失败"));
    }

    @Override
    public ResponseResult getLoginInfo(HttpServletRequest request) {
        String token = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = this.getById(token);
        Map<String, UcenterMember> map = new HashMap<>();
        map.put("userInfo",member);
        return ResponseResult.ofSuccess(map);
    }

    @Override
    public Integer countRegisterByDay(String day) {
        Integer integer = baseMapper.selectRegisterCount(day);
        return integer;
    }
}
